﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Updater;
using System.Collections.Generic;

namespace Test
{
    public partial class Form1 : Form
    {
        private int applicableManifests;
        private List<Manifest> manifestToBeApplied;
        private ApplicationUpdaterManager mUpdateManager;
        internal ApplicationUpdaterManager UpdateManager
        {
            get
            {
                return mUpdateManager;
            }
        }



        #region Constructor
        public Form1()
        {
            InitializeComponent();

            mUpdateManager = ApplicationUpdaterManager.GetUpdater();
            applicableManifests = 0;
            manifestToBeApplied = new List<Manifest>();
        }
        #endregion

        
        private void updateButton_Click(object sender, System.EventArgs e)
        {
            Button btn = (Button)sender;
            try
            {
                Manifest[] manifests = UpdateManager.CheckForUpdates();
                applicableManifests = manifests.Length;
                if (manifests.Length > 0)
                {
                    // Prompt user if he wants to apply the updates
                    if (MessageBox.Show(this, "Update for Manual Inproc Application is available, do you want to apply the update?", "Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        foreach (Manifest m in manifests)
                        {
                            m.Apply = true;
                        }

                        btn.Enabled = false;

                        // download the updates asychronously.
                        UpdateManager.BeginDownload(manifests);

                    }
                }
                else
                {
                    MessageBox.Show(this, "There are no updates available for this application.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    
    }
}
